﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.IO;
using Microsoft.Phone;
using System.Windows.Media.Imaging;

namespace ActivityTracker.Library
{
    public class UserImage
    {
        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

        public void SaveImage(string imagename, string imageFolder, byte[] _imageBytes)
        {
            try
            {
                if (_imageBytes == null)
                {
                    return;
                }
                var isoFile = IsolatedStorageFile.GetUserStoreForApplication();
                if (!isoFile.DirectoryExists(imageFolder))
                {
                    isoFile.CreateDirectory(imageFolder);
                }
                string filePath = System.IO.Path.Combine(imageFolder, imagename);
                using (var stream = isoFile.CreateFile(filePath))
                {
                    stream.Write(_imageBytes, 0, _imageBytes.Length);
                }
            }
            catch (Exception ex)
            {
                Exception exnew = new Exception("Exception in SaveImage Method In ActivityTracker.Library.Themes.UserImage file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace);
                exnew.Data.Add("imagename", imagename);
                exnew.Data.Add("imageFolder", imageFolder);
                throw exnew;
            }
        }

        public void DeleteImage(string imgName)
        {
            try
            {
                if (isoStore.DirectoryExists("ActivityTracker/ActivityImages"))
                {
                    string filePath = System.IO.Path.Combine("ActivityTracker/ActivityImages", imgName);
                    if (isoStore.FileExists(filePath))
                        isoStore.DeleteFile(filePath);
                }
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in DeleteImage Method In ActivityTracker.Library.Themes.UserImage file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                exNew.Data.Add("imgName_DeleteImage", imgName);
                throw exNew;
            }
        }

        public object LoadActivityImages(string actId, string imgName)
        {
            object obj = null;
            string filePath = "";
            string fp = "";
            int id = 0;
            try
            {
                id = Convert.ToInt32(actId);
                filePath = System.IO.Path.Combine("ActivityTracker/ActivityImages", imgName);
                if (isoStore.FileExists(filePath))
                {
                    using (var imageStream = isoStore.OpenFile(filePath, FileMode.Open, FileAccess.Read))
                    {
                        var imageSource = PictureDecoder.DecodeJpeg(imageStream);
                        obj = imageSource as object;
                    }
                }
                if (obj == null)
                {
                    fp = "/Images/ActivityImages/" + imgName;
                    BitmapImage bitmapImage = new BitmapImage();
                    bitmapImage.UriSource = new Uri(fp, UriKind.Relative);
                    obj = bitmapImage;
                    bitmapImage = null;
                }
                if (id == 0)
                {
                    fp = "/Images/ActivityImages/AddImage.png";
                    BitmapImage bitmapImage = new BitmapImage(new Uri(fp, UriKind.Relative));
                    obj = bitmapImage;
                    bitmapImage = null;
                }
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in LoadActivityImages Method In  ActivityTracker.Library.Themes.UserImage file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                exNew.Data.Add("actId_LoadActivityImages", actId);
                exNew.Data.Add("imgName_LoadActivityImages", imgName);
                throw exNew;
            }
            return obj;
        }
    }
}
